/+
Library dbapi - Simple database api for the D programming language.

Copyright (C) 2007 Myron Alexander (myron.alexander...gmail.com)

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+/

/**
 * A module that contains helper functions.
 */

module dbapi.helper;

import std.boxer;
import dbapi.base;

/**
 * Extract an int value from the supplied box.
 *
 * If the box value is null, then the supplied default value is returned.
 */
int xint (Box value, int def = int.init) {
   return unboxNullDefault !(int) (value, def);
}


/**
 * Extract an int value at idx of the supplied box array.
 *
 * If the box value at idx is null, then the supplied default value is returned.
 */
int xint (Box[] values, int idx, int def = int.init) {
   return unboxNullDefault !(int) (values [idx], def);
}


/**
 * Extract a float value from the supplied box.
 *
 * If the box value is null, then the supplied default value is returned.
 */
float xflt (Box value, float def = float.init) {
   return unboxNullDefault !(float) (value, def);
}


/**
 * Extract a float value at idx of the supplied box array.
 *
 * If the box value at idx is null, then the supplied default value is returned.
 */
float xflt (Box[] values, int idx, float def = float.init) {
   return unboxNullDefault !(float) (values[idx], def);
}


/**
 * Extract a double value from the supplied box.
 *
 * If the box value is null, then the supplied default value is returned.
 */
double xdbl (Box value, double def = double.init) {
   return unboxNullDefault !(double) (value, def);
}


/**
 * Extract a double value at idx of the supplied box array.
 *
 * If the box value at idx is null, then the supplied default value is returned.
 */
double xdbl (Box[] values, int idx, double def = double.init) {
   return unboxNullDefault !(double) (values[idx], def);
}


/**
 * Extract a string value from the supplied box.
 *
 * If the box value is null, then the supplied default value is returned.
 */
char[] xstr (Box value, char[] def = "") {
   return unboxNullDefault !(char[]) (value, def);
}


/**
 * Extract a string value at idx of the supplied box array.
 *
 * If the box value at idx is null, then the supplied default value is returned.
 */
char[] xstr (Box[] values, int idx, char[] def = "") {
   return unboxNullDefault !(char[]) (values[idx], def);
}


/**
 * Extract a blob value from the supplied box.
 *
 * If the box value is null, then the supplied default value is returned.
 */
void[] xblb (Box value, void[] def = null) {
   return unboxNullDefault !(void[]) (value, def);
}


/**
 * Extract a blob value at idx of the supplied box array.
 *
 * If the box value at idx is null, then the supplied default value is returned.
 */
void[] xblb (Box[] values, int idx,void[] def = null) {
   return unboxNullDefault !(void[]) (values[idx], def);
}
